<template>
	<view class="container-me" v-if="userInfo">
	  <view class="header">
	    <view>账号: {{userInfo.username}}</view>
	    <view>级别: {{userInfo.group_title}}</view>
	    <view>注册日期: {{userInfo.created_at}}</view>
	  </view>
	  <view class="body">
	    <view class="bottom">
	      <view class="kefu">专属客服: {{userInfo.kefu_name}}</view>
	      <view class="mobile">客服电话: {{userInfo.kefu_mobile}}</view>
	    </view>
	  </view>
	  <view class="bottom">
	    <button type="warn" @click="loginOut">退出登录</button>
	  </view>
	</view>
</template>

<script>
	export default{
		data(){
			return {
				userInfo: null,
				currentSchool: {}
			}
		},
		 onShow (options) {
			//概览会切换数据,所以没次显示更新数据
			this.getSchools()
		  },
		methods:{
			getSchools(){
			    var school = this.$store.getters.currentSchool;
			    var userInfo = this.$store.getters.userInfo;
			    //学校存在且用户专属客服存在
			    if(school && userInfo && userInfo.hasOwnProperty('kefu_name')) {
			      this.getUserInfo()
			      return;
			    }
			
				this.$api.getSchoolList().then((rep)=>{
					let {data} = rep.data
			        let school = data.length ? data[0] : null;
			        if (school){
			          this.sysncAppCurrentSchool(school)
			          this.getUserInfo()
			        }
			      })
			  },
			
			  getUserInfo () {
			    let school = this.$store.getters.currentSchool
			    //未切换学校
			    if (school.school_id == this.currentSchool.school_id){
			      return;
			    }
			    this.requestUserInfo(school)
			
			  },
			
			  requestUserInfo(school){
				this.$api.getUserInfo({school_id: school.school_id}).then((rep)=>{
					let {data} = rep.data
					this.userInfo = data
					this.sysncCurrentSchool(school)
					this.$store.commit("SET_INFO",data)
				})
			    
			  },
			
			  sysncAppCurrentSchool(school){
			    this.$store.commit("SET_SCHOOL",JSON.stringify(school))
				  console.log("sysncAppCurrentSchool")
			  },
			
			  sysncCurrentSchool(school){
			    console.log(school)
				this.currentSchool = school
			  },
			
			  loginOut(){
				this.$store.dispatch("resetToken").then(()=>{
					uni.showToast({
					  icon: 'none',
					  title: '已清除个人数据',
					  success :()=>{
						uni.reLaunch({
						  url: '../index/index',
						})
					  }
					})
				})
			  }
		}
		
	}
</script>

<style lang="scss">
	page{
	  height: 100%;
	}
	
	.container-me{
	  height: 100%;
	  display: flex;
	  flex-direction: column;
	}
	
	.header{
	  display: flex;
	  flex-direction: column;
	  padding: 15px;
	}
	
	.header > view {
	  height: 80rpx;
	  display: flex;
	  align-items: center;
	  font-size: 14px;
	}
	
	.header >view:nth-child(1){
	  font-weight: bold;
	  font-size: 18px;
	}
	
	.body{
	  background-color: #F5F5F5;
	  margin-top: 80rpx;
	  margin-left: 50rpx;
	  margin-right: 50rpx;
	  height: 200rpx;
	  font-size: 14px;
	  display: flex;
	  justify-content: center;
	  align-items: center;
	}
	
	.kefu, .mobile{
	  height: 60rpx;
	  text-align: center;
	  line-height: 60rpx;
	}
	.bottom{
	  margin-top: 20rpx;
	  margin-bottom: 20rpx;
	  width: 100%;
	}
</style>
